package com.gcph.zhuoba.player.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.gcph.zhuoba.R;
import com.gcph.zhuoba.main.bean.Resource;
import com.gcph.zhuoba.main.bean.ResourceData;
import com.gcph.zhuoba.main.common.Constant;
import com.gcph.zhuoba.util.DownImage;
import com.gcph.zhuoba.util.L;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
/**
 * 在线求助详情页面
 * @author zhanghejie
 */
public class OnlineHelpDetailActivity extends Activity {
	private String resourceId;// 资源ID
	private String resourceDesc;// 资源描述
	
	//我要帮忙
	private Button doButton;
	private ImageButton back;
	private ImageButton pub;
	private TextView resource_type_text;
	private ImageView resourceDetailImg;
	private Resource resource; 
	private ResourceData gesourceData;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_onlinehelp_detail);
		Intent intent=getIntent();
		resourceId=intent.getStringExtra("resourceId");
		resourceDesc=intent.getStringExtra("resourceDesc");
		
		TextView descTextView = (TextView) findViewById(R.id.resource_detail_desc);
		descTextView.setText(resourceDesc);
		
		//初始化按钮，并设置监听
		viewInitAndSetClickListener();
		
		new GetResourceTask().execute();
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	/**
	 * 初始化按钮，并设置监听
	 */
	private void viewInitAndSetClickListener()
	{
		doButton = (Button) findViewById(R.id.resource_detail_do);
		resource_type_text = (TextView) findViewById(R.id.resource_type_text);
		//默认为不可用的，等教案信息加载完以后变为可用
		doButton.setEnabled(false);
		
		doButton.setTag(resourceId);
		back = (ImageButton) findViewById(R.id.btn_back);
		pub = (ImageButton) findViewById(R.id.btn_share);
		
		resourceDetailImg = (ImageView) findViewById(R.id.resource_detail_img);
		// 设置返回按钮事件
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				OnlineHelpDetailActivity.this.finish();
			}
		});
		// 设置发布分享按钮事件
		pub.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

			}
		});
		
		//单击我帮忙按钮,去我要帮忙界面
		doButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(OnlineHelpDetailActivity.this,OnlineAnswerActivity.class);
				//把当前资源传过去
				Bundle bundel = new Bundle();
				bundel.putSerializable("resource", resource);
				intent.putExtras(bundel);
				startActivity(intent);
			}
		});
	}
	
	/**
	 * 异步读取教案信息
	 * @author zhanghejie
	 */
	private class GetResourceTask extends AsyncTask<Void, Void, Void> {
		@Override
		protected Void doInBackground(Void... params) {
			HttpUtils http = new HttpUtils();
			http.configCurrentHttpCacheExpiry(0l);//不缓存请求数据
			
			RequestParams httpParams=new RequestParams();
			httpParams.addBodyParameter("id", resourceId);
			http.send(HttpRequest.HttpMethod.POST, getResources().getString(R.string.api_resource_get),httpParams,
					new RequestCallBack<String>() {
						@Override
						public void onSuccess(ResponseInfo<String> responseInfo) {
							
							String response = responseInfo.result;
							L.d("GetResourceTask response=="+response);
							resource = new Gson().fromJson(response, Resource.class);
							if(null != resource)
							{
								// 如果存在网络图片，需要网络加载
								DownImage downImage = new DownImage(resourceDetailImg);
								downImage.execute(getResources().getString(R.string.server_root_url) + resource.getImgFileUrl());
								
								gesourceData =  new Gson().fromJson(resource.getResourceData(), ResourceData.class);
								
								L.d("resource id=="+resource.getId());
								L.d("resource imgFileUrl=="+resource.getImgFileUrl());
								L.d("gesourceData=="+gesourceData);
								resource_type_text.setText(Constant.getResourceText(resource.getResourceType(), resource.getResourceSmallType()));
								doButton.setEnabled(true);
							}
						}

						@Override
						public void onFailure(HttpException error, String msg) {
						}
					});
			
			return null;
		}
	}
}